package com.grad.why.studentsystem.controller;

import com.grad.why.studentsystem.domain.ClassRoom;
import com.grad.why.studentsystem.domain.CourseArrange;
import com.grad.why.studentsystem.domain.Student;
import com.grad.why.studentsystem.domain.current.CurrentCourseArrangeInfo;
import com.grad.why.studentsystem.service.ChooseCourseService;
import com.grad.why.studentsystem.service.CourseArrangeService;
import com.grad.why.studentsystem.service.OptionparamsService;
import com.grad.why.studentsystem.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/studentManageSystem")
@Controller
public class CourseArrangeController {
    @Autowired
    private CourseArrangeService courseArrangeService;

    @Autowired
    private ChooseCourseService chooseCourseService;

    @Autowired
    private OptionparamsService optionparamsService;


    @RequestMapping("/getCourseArrangeInfos")
    @ResponseBody
    public Object getCourseArrangeInfoList(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search
    ) {
        try {
            List<CurrentCourseArrangeInfo> rows = courseArrangeService.getCourseArrangeInfoList(offset,limit,search);
            for(int i = 0;i<rows.size();i++){
                List<Student> stuInfos = chooseCourseService.getStudentByCourseId(rows.get(i).getCid());
                StringBuffer sb = new StringBuffer("");
                int stuLength = stuInfos.size();
                for(int j = 0;j<stuLength;j++){
                    Student stu = stuInfos.get(j);
                    sb.append(stu.getId()+"_"+stu.getName()+",");
                }
                String studentList = sb.toString();

                if(sb.toString().equals("")){
                    rows.get(i).setShouldnamelabels("");
                }else{
                    rows.get(i).setShouldnamelabels(sb.toString().substring(0,studentList.length() - 1));
                }
                rows.get(i).setShouldcount(stuLength);
            }

            long total = courseArrangeService.getCourseArrangeInfoListLength(search);
            Map<String, Object> result = new HashMap<>(2);
            result.put("rows", rows);
            result.put("total", total);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/addCourseArrange")
    @ResponseBody
    public String addCourseArrange(CourseArrange courseArrange){
        try{
            int maxId = courseArrangeService.getMaxId() + 1;
            int n = courseArrangeService.addCourseArrangeByEntity(courseArrange);
            if(n<=0){
                return null;
            }else{
                return "OK";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/updateCourseArrange")
    @ResponseBody
    public String updateCourseArrange(CourseArrange courseArrange){
        try{
            int n =  courseArrangeService.updateCourseArrangeInfoByEntity(courseArrange);
            if(n>0){
                return "OK";
            }else{
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/deleteCourseArrangeByIds/{ids}")
    @ResponseBody
    public String deleteCourseArrangeByIds(@PathVariable("ids")String ids){
        try{
            for(String id:ids.split("-")){
                int n = courseArrangeService.deleteCourseArrangeInfosById(Integer.parseInt(id));
                if(n<=0){
                    return null;
                }
            }
            return "OK";
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/uploadCourseArrangeInfos")
    @ResponseBody
    public String uploadStudentInfos(@RequestParam("file") MultipartFile file){
        try{
            String fileUploadRootPath = optionparamsService.getParamValueByParamName("fileRootPath");
            String fileName = file.getOriginalFilename();
            String uploadCurrentFileName = fileUploadRootPath+"uploadCourseInfo/"+ new Date().getTime() + fileName;

            File fileRootPath = new File(fileUploadRootPath+"uploadCourseInfo");

            if(!fileRootPath.exists()){
                fileRootPath.mkdirs();
            }

            FileOutputStream out = new FileOutputStream(uploadCurrentFileName);
            out.write(file.getBytes());
            out.flush();
            out.close();

            int n = courseArrangeService.addCourseArrangeInfosByExcelFile(uploadCurrentFileName);
            if(n<=0){
                return "添加失败";
            }else{
                return "添加"+n+"条数据";
            }
            //上传学生信息逻辑
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
